<?php
namespace Home\Controller\Mobile;
use Home\Controller\Mobile\BasicController;

class HomeController extends BasicController {
	
	public function my(){
		$userdata = session('user');
		$uid = $userdata['uid'];
		$flag = 'system';
		$par['flag'] = $flag;
		$sys = M('sxkj_offline_data')->where($par)->find();
		$sys = json_decode($sys['data'],true);
		//用户信息
		$userinfo = M('sxkj_offline_user')->where('id=%d',$uid)->find();
		
		$this->assign('userinfo',$userinfo);
		$this->assign('cid',$cid);
		$this->assign('sys',$sys);
		$this->assign('project_class',$project_class);
		$this->display();
	}
 	
 	public function income(){
 		$userdata = session('user');
 		$uid = $userdata['uid'];
 		$where['e.uid'] = $uid;
 		$where['e.status'] = 1;
 		$list = M('sxkj_offline_examine e')->field("e.*,p.title,p.taskid")
 				->join(C('DB_PREFIX')."sxkj_offline_order o ON e.orderid=o.id",'LEFT')
				->join(C('DB_PREFIX')."sxkj_offline_project p ON o.pid=p.id",'LEFT')
				->where($where)->order('e.id desc')->select();
				
		$this->assign('list',$list);
 		$this->display();
 	}
 	
 	public function apply(){
 		$userdata = session('user');
 		$uid = $userdata['uid'];
 		$par['uid'] = $uid;
 		$jxscode = I('request.jxscode');
 		$list = M('sxkj_offline_userteam')->where($par)->find();
   		if($list['status']==3 || true){
   			header('Location:'.$this->get_http().'://'.$_SERVER['HTTP_HOST'].U('Home/Mobile/Home/team'));exit;
   		}
 		//系统设置
		$par['flag'] = 'system';
		$system = M('sxkj_offline_data')->where($par)->find();
		$system = json_decode($system['data'],true);
		
		$this->assign('system',$system);
 		$this->assign('jxscode',$jxscode);
 		$this->assign('list',$list);
 		$this->display();
 	}
	
 	public function do_apply(){
 		$userdata = session('user');
 		$uid = $userdata['uid'];
 		$data['uid'] = $uid;
 		$data['phone'] = I('post.phone');
 		$data['true_name'] = I('post.true_name');
 		$data['remark'] = I('post.remark');
		$data['jxscode'] = I('post.jxscode');
 		$data['add_time'] = time();
 		$data['status'] = 1;
 		$res = M('sxkj_offline_userteam')->add($data);
 		
 		if($res!==false){
 			echo '1';exit;
 		}else{
 			echo '2';exit;
 		}
 	}
	
	public function team(){
		$userdata = session('user');
		$uid = $userdata['uid'];
		//用户信息
		$userinfo = M('sxkj_offline_user')->where('id=%d',$uid)->find();
		//订单数
		$all_order = M('sxkj_offline_order')->where('uid=%d',$uid)->count();
		
		$s_time = strtotime(date('Y-m-d'));
		$e_time = $s_time+86399;
		$par['uid'] = $uid;
		$par['add_time'] = array(array('egt',$s_time),array('elt',$e_time),'and');
		$today_order = M('sxkj_offline_order')->where($par)->count();
		
		$this->assign('today_order',$today_order);
		$this->assign('all_order',$all_order);
		$this->assign('userinfo',$userinfo);
		$this->display();
	}
	
	//提现
	public function widthdrawal(){
		$userdata = session('user');
		$uid = $userdata['uid'];
		//用户信息
		$userinfo = M('sxkj_offline_user')->where('id=%d',$uid)->find();
		
		$this->assign('userinfo',$userinfo);
		$this->display();
	}
	
	public function do_widthdrawal(){
		$userdata = session('user');
		$uid = $userdata['uid'];
		$data['uid'] = $uid;
		$data['true_name'] = I('post.true_name');
		$data['cash'] = number_format(I('post.cash'),2,".","");
		$data['zfb'] = I('post.zfb');
		$data['add_time'] = time();
		$data['status'] = 1;
		$res = M('sxkj_offline_txrecord')->add($data);
		
		if($res!==false){
			$mycash = M('sxkj_offline_user')->where('id=%d',$uid)->getField('cash');
			$data_user['cash'] = number_format($mycash-$data['cash'],2,".","");
			M('sxkj_offline_user')->where('id=%d',$uid)->save($data_user);
			echo '1';exit;
		}else{
			echo '2';exit;
		}
	}
	//提现记录
	public function record(){
		$userdata = session('user');
		$uid = $userdata['uid'];
		$where['uid'] = $uid;
		$list = M('sxkj_offline_txrecord t')->field("t.*,u.nickname")
				->join(C('DB_PREFIX')."sxkj_offline_user u ON t.uid=u.id",'LEFT')
				->where($where)->order('t.id desc')->select();
				
		$this->assign('list',$list);
		$this->display();
	}
	//下级人员
	public function personnel(){
		$userdata = session('user');
		$uid = $userdata['uid'];
		$par['u.sup_promoterid'] = $uid;
		$list = M('sxkj_offline_user as u')->where($par)->select();
		
		foreach($list as $key=>$val){
			$list[$key]['order_num'] = M('sxkj_offline_order')->where('uid=%d',$val['id'])->count();
			$list[$key]['all_reprice'] = M('sxkj_offline_order')->where('uid=%d',$val['id'])->sum('reprice');
		}
		
		$this->assign('list',$list);
		$this->display();
	}
	
	public function poster(){
		$flag = I('request.flag');
		$pid = I('request.pid');
		
		$this->assign('pid',$pid);
		$this->assign('flag',$flag);
		$this->display();
	}
	
	//推广海报
	public function view_poster(){
		$userdata = session('user');
		$user = M('sxkj_offline_user')->field('avatar,nickname,jxscode')->where('id=%d',$userdata['uid'])->find();
		//获取路径
		header ("Content-type: image/png");

		$par['flag'] = 'system';
		$system = M('sxkj_offline_data')->where($par)->find();
		$system = json_decode($system['data'],true);
		$bj = $system['posters'];

		$flag = I('request.flag');
		if($flag==1){
			$rkurl = $this->get_http().'://'.$_SERVER['HTTP_HOST'].U('Home/Mobile/Index/index',array('jsxcode'=>$user['jxscode']));
		}else{
			$pid = I('request.pid');
			$rkurl = $this->get_http().'://'.$_SERVER['HTTP_HOST'].U('Home/Mobile/Detail/detail',array('jsxcode'=>$user['jxscode'],'id'=>$pid));
		}
		
		list($width,$height,$type)=getimagesize($bj);
		if($type == 1){  
			$src_im = imagecreatefromgif($bj); //gif  
		}else if($type == 2){  
			$src_im = imagecreatefromjpeg($bj); //jpg  
		}else if($type == 3){  
			$src_im = imagecreatefrompng($bj); //png  
		}else{  
		   exit;
		}

		require_once (dirname(__FILE__)."/../../../../php/phpqrcode.php");
		$ewmwidth = 250;
		$ewmheight = 250;	
		$QRcode = new \QRcode();
		
		$filename = 'qrewm.png';
		$QRcode::png($rkurl,$filename,'L', 10, 2,false);
		$QR = $filename;
		
		$src_im1 = imagecreatefromstring(file_get_contents($QR));
		$dst_im = imagecreatetruecolor($ewmwidth, $ewmheight);
		//二维码
		$ewms = 970;
		$ewmz = 253;
		@imagecopyresized($dst_im, $src_im1, 0, 0, 0, 0, $ewmwidth, $ewmheight, 430, 430);
		imagecopymerge($src_im, $dst_im, $ewmz, $ewms, 0, 0,$ewmwidth ,$ewmheight, 100);
		imagejpeg($src_im);		
		$img = ob_get_contents();
		imagedestroy($src_im);
	}
}